package com.xqls.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.xqls.bean.RegisterBean;
import com.xqls.biz.CompanyBiz;
import com.xqls.biz.DictionaryBiz;
import com.xqls.biz.RegisterBiz;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.DateFormatter;
import com.xqls.util.DictionaryEnums;
import com.xqls.util.GetKeyByValue;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RequestMapping(value="/register")
@Controller
public class RegisterController {
	
	@Autowired private RegisterBiz registerBiz;
	
	@Autowired
	private CompanyBiz companyBiz;
	
	@Autowired 
	private DictionaryBiz dictionaryBiz;
	
	@RequestMapping(value="/index")
	public ModelAndView index(){
		ModelAndView view = new ModelAndView("register/registerIndex");
		Map<Integer, String> map = dictionaryBiz.queryDictionaryByParent(DictionaryEnums.COMPANY_TYPE.getCode());
		view.addObject("deviceType", GetKeyByValue.getKey(map, "取水口"));
		return view;
	}
	
	@RequestMapping(value="/input")
	public ModelAndView input(){
		ModelAndView view = new ModelAndView("register/registerInput");
		return view;
	}
	
	@RequestMapping(value = "/registerListData")
	@ResponseBody
	public CommonResult registerListData(@RequestBody CommonPage commonPage, HttpServletRequest request, HttpServletResponse response){
		CommonResult result = registerBiz.getRegisByPage(commonPage,request,response);
		if(result.getIsSuccess()){
			result.setData(commonPage);
		}
		return result;
	}
	
	@RequestMapping(value = "/edit/{id}")
	public ModelAndView edit(@PathVariable(value="id") Integer samplingId){
		ModelAndView view = new ModelAndView("register/registerEdit");
		System.out.println(samplingId);
		if(samplingId != null && !"".equals(samplingId)){
			CommonResult commonResult = registerBiz.queryByKey(samplingId);
			RegisterBean bean=(RegisterBean) commonResult.getData();
			bean.setSamplingTimeStr(DateFormatter.format(bean.getSamplingTime()));
			if(bean != null){
				view.addObject("bean", bean);
			}
		}
		view.addObject("companyList", companyBiz.getList(DictionaryEnums.INTAKE));
		return view;
	}
	
	@RequestMapping("/savec")
	@ResponseBody
	public String savec(RegisterBean bean ){
		return JSONObject.toJSONString(registerBiz.editc(bean));
	}
	
	@RequestMapping("/saveput")
	@ResponseBody
	public String saveput(RegisterBean bean ){
		return JSONObject.toJSONString(registerBiz.editsave(bean));
	}
	
	@RequestMapping(value="/remove")
	@ResponseBody
	public String remove(@RequestBody List<Integer> ids){
		return JSONObject.toJSONString(registerBiz.removeList(ids));
	}
	@RequestMapping(value="/noChange")
	@ResponseBody
	public String noChange(@RequestBody String no){ //先查询
	no=no.substring(0,no.length()-1);
	CommonResult commonResult= registerBiz.queryByKey(Integer.valueOf(no));
	RegisterBean bean=(RegisterBean) commonResult.getData();
	if(bean!=null){
		bean.setSamplingTimeStr(DateFormatter.format(bean.getSamplingTime()));
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("NH3", bean.getNh3());
		map.put("PH", bean.getPh());
		map.put("COD", bean.getCod());
		map.put("samplingMan", bean.getSamplingMan());
		map.put("samplingTime", bean.getSamplingTimeStr());
		map.put("samplingId", bean.getSamplingId());
		return JSONObject.toJSONString(map);
	}
	return null;
	}
}
